#!/usr/bin/env python

import sys
from lib.format_time import *
from shop_conn import *
from lib.HandleMysql import *
from lib.switch_code import *
import re
import os

sys.setdefaultencoding('utf-8')


def select_current_album(date):
    """

    :param date:
    """
    shop_conn = set_shop_conn()
    shop_id = 0
    query = "select distinct shopid from album where left(adddate,10)='%s'" % date
    print "select_current_pic %s" % query
    items = shop_conn.get_results(query)
    if len(items) > 0:
        print "today we'll update %s shops" % len(items)
    for item in items:
        shop_id = item[0]
        if shop_id != 0:
            get_album_and_update_stats(shop_id)


def get_album_and_update_stats(shop_id):
    """

    :param shop_id:
    """
    shop_conn = set_shop_conn()
    album_count = 0
    sql_album = "select count(1) from album where shopid = %s" % shop_id
    items_album = shop_conn.get_results(sql_album)
    try:
        album_count = items_album[0][0]
    except Exception, e:
        album_count = 0

    stat_conn = set_stats_conn()
    if album_count != 0:
        print "shop:%s have %s albums" % (shop_id, album_count)
        sql = "update shop_info set album_count = %s  where shop_id = %s;" % (album_count, shop_id)
        stat_conn.execute_sql(sql)
        print 'update success.'
    else:
        print 'album count equal 0 '


def select_current_pic(date):
    """

    :param date:
    """
    shop_conn = set_shop_conn()
    shop_id = 0
    query = "select distinct shopid from pic where state = 0 and left(adddate,10)='%s'" % date
    print "select_current_pic %s" % query
    items = shop_conn.get_results(query)
    if len(items) > 0:
        print "today we'll update %s shops" % len(items)
    for item in items:
        shop_id = item[0]
        if shop_id != 0:
            get_pics_and_update_stats(shop_id)


def get_pics_and_update_stats(shop_id):
    """

    :param shop_id:
    """
    shop_conn = set_shop_conn()
    pic_count = 0
    sql_pic = "select count(1) from pic where state=0 and  shopid = %s" % shop_id
    items_pic = shop_conn.get_results(sql_pic)
    try:
        pic_count = items_pic[0][0]
    except Exception, e:
        pic_count = 0
    stat_conn = set_stats_conn()
    if pic_count != 0:
        print "shop:%s have %s pics" % (shop_id, pic_count)
        sql = "update shop_info set pic_count = %s where shop_id = %s " % (pic_count, shop_id)
        stat_conn.execute_sql(sql)
        print 'update success.'
    else:
        print 'pic count equal 0 '


if __name__ == '__main__':
    date = ""
    if len(sys.argv) == 2:
        date = format_date(sys.argv[1])
        select_current_album(date)
        select_current_pic(date)
    elif len(sys.argv) > 2:
        print len(sys.argv)
        print sys.argv[1]
        start_date = format_date(sys.argv[1])
        print "start_date = %s" % start_date
        end_date = format_date(sys.argv[2])
        print "end_date = %s" % end_date
        stat_date = start_date
        while (stat_date >= start_date) and (stat_date <= end_date):
            print "The program of the date of %s is beginning at %s " % (stat_date, get_now())
            select_current_album(date)
            select_current_pic(date)
            stat_date = var_date(stat_date)
            print "stat_date = %s" % stat_date
            print "The program of the date of %s is end at %s " % (stat_date, get_now())
    else:
        date = get_yesterday()
        select_current_album(date)
        select_current_pic(date)
